"""
数据模型类
"""
from datetime import datetime
from typing import Optional
from dataclasses import dataclass, field
from . import config


@dataclass
class Request:
    """空调请求"""
    room_id: str  # 房间号
    target_temp: float  # 目标温度
    wind_speed: str  # 风速
    mode: str  # 模式（制冷/制热）
    timestamp: datetime = field(default_factory=datetime.now)  # 请求时间
    
    def get_priority(self) -> int:
        """获取请求优先级"""
        return config.WIND_PRIORITY.get(self.wind_speed, 0)


@dataclass
class ServiceStatus:
    """服务状态"""
    room_id: str  # 房间号
    service_object_id: int  # 服务对象ID
    wind_speed: str  # 风速
    service_duration: float = 0.0  # 服务时长（秒）
    start_time: datetime = field(default_factory=datetime.now)  # 开始服务时间
    
    def update_duration(self, current_time: datetime):
        """更新服务时长"""
        self.service_duration = (current_time - self.start_time).total_seconds()


@dataclass
class WaitingStatus:
    """等待状态"""
    room_id: str  # 房间号
    wind_speed: str  # 风速
    waiting_duration: float  # 已等待时长（秒）
    allocated_wait_time: float  # 分配的等待时长（秒）
    start_wait_time: datetime = field(default_factory=datetime.now)  # 开始等待时间
    priority_boosted: bool = False  # 是否优先级提升（等待满s秒后）
    
    def update_waiting_duration(self, current_time: datetime):
        """更新等待时长"""
        self.waiting_duration = (current_time - self.start_wait_time).total_seconds()
    
    def is_ready(self, current_time: datetime) -> bool:
        """是否已等待足够时间"""
        self.update_waiting_duration(current_time)
        return self.waiting_duration >= self.allocated_wait_time


@dataclass
class BillingDetail:
    """详单记录"""
    room_id: str
    start_time: datetime
    end_time: datetime
    wind_speed: str
    start_temp: float
    end_temp: float
    target_temp: float
    duration_minutes: float  # 持续时间（分钟）
    power_consumption: float  # 耗电量（度）
    cost: float  # 费用（元）
    

@dataclass
class RoomState:
    """房间状态"""
    room_id: str
    current_temp: float  # 当前温度
    target_temp: Optional[float] = None  # 目标温度
    wind_speed: Optional[str] = None  # 当前风速
    mode: str = config.MODE_COOLING  # 当前模式
    is_on: bool = False  # 是否开机
    is_serving: bool = False  # 是否正在服务
    is_waiting: bool = False  # 是否在等待
    total_cost: float = 0.0  # 总费用
    total_power: float = 0.0  # 总耗电量
    last_update_time: datetime = field(default_factory=datetime.now)  # 上次更新时间
    check_in_time: Optional[datetime] = None  # 入住时间
    billing_details: list = field(default_factory=list)  # 详单列表
    
    def add_billing_detail(self, detail: BillingDetail):
        """添加详单记录"""
        self.billing_details.append(detail)
        self.total_power += detail.power_consumption
        self.total_cost += detail.cost

